﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.RefundCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;
using System.Linq;

namespace D.Medicalcare.Read.API.Application.Handler.RefundHandler
{
    public class GetRefundHandler : IRequestHandler<GetRefundCommand, APIPaging<RefundDto>>
    {
        private readonly IBaseRepository<Refund> RefundbaseRequest;
        private readonly IBaseRepository<Patient> PatientbaseRequest;

        public GetRefundHandler(IBaseRepository<Refund> refundbaseRequest, IBaseRepository<Patient> patientbaseRequest)
        {
            RefundbaseRequest = refundbaseRequest;
            PatientbaseRequest = patientbaseRequest;
        }

        public Task<APIPaging<RefundDto>> Handle(GetRefundCommand request, CancellationToken cancellationToken)
        {
            APIPaging<RefundDto> result = new APIPaging<RefundDto>
            {
                Code = APIEnum.Mvc,
                Msg = "查询成功"
            };

            var list = from a in RefundbaseRequest.GetAll()
                       join b in PatientbaseRequest.GetAll() on a.PatientId equals b.PatientId
                       where a.IsDeleted == false
                       select new RefundDto
                       {
                           RefundId = (int)a.RefundId,
                           PatientId = (int?)b.PatientId,
                           Impact = b.Impact,
                           PatientName = b.PatientName,
                           DocumentType = b.DocumentType,
                           DocumentNo = b.DocumentNo,
                           PatientNo = b.PatientNo,
                           Phone = b.Phone,
                           Address = b.Address,
                           ParentId = b.ParentId,
                           RefundAmount = (double?)a.RefundAmount,
                           RefundPath = a.RefundPath,
                           CardBalance = (double?)a.CardBalance,
                           RefundStatus = a.RefundStatus,
                           RefundNo = a.RefundNo,
                           RefundTime = a.RefundTime,
                           Source = a.Source,
                           BusinessType = a.BusinessType,
                           Remark = a.Remark,
                           CreateTime = a.CreateTime
                       };


            if (!string.IsNullOrEmpty(request.PatientName))
            {
                list = list.Where(x => x.PatientName == request.PatientName);
            }

            if (!string.IsNullOrEmpty(request.PatientNo))
            {
                list = list.Where(x => x.PatientNo == request.PatientNo);
            }

            if (request.CreateTime != null)
            {
                list = list.Where(x => x.RefundTime >= request.CreateTime);
            }

            if (request.EndTime != null)
            {
                list = list.Where(x => x.RefundTime <= request.EndTime);
            }

            result.Count = list.Count();
            result.PageCount = (int)Math.Ceiling(result.Count * 1.0 / request.PageSize);
            result.Data = list.OrderByDescending(a => a.RefundId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
    }
}